Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace WebControls

    <ToolboxData("<{0}:MyRegularExpressionValidator runat=server></{0}:MyRegularExpressionValidator>")> _
Public Class MyRegularExpressionValidator
        Inherits RegularExpressionValidator
 
        Private Const MoneyExpression As String = "(^[1-9]\d*(\.\d{1,3})?$)|(^0\.(([1-9]{1,3})|(\d{1}[1-9])|([1-9]\d{1}))$)"
        Private Const MoneyErrorMessage As String = "Invalid input.<br/>Input a amount such as 0.99."
        Private Const negativeMoneyExpression As String = "(^\-?[1-9]\d*(\.\d{1,3})?$)|(^\-?0\.(([1-9]{1,3})|(\-?\d{1}[1-9])|(\-?[1-9]\d{1}))$)"

        <Category("Misc")> _
        Public Property IsValidMoney() As Boolean
            Set(ByVal value As Boolean)
                If value Then
                    Me.ValidationExpression = MoneyExpression
                Else
                    Me.ValidationExpression = ""
                End If
            End Set
            Get
                Return Me.ValidationExpression = MoneyExpression OrElse Me.ValidationExpression = negativeMoneyExpression
            End Get
        End Property

        <Category("Misc")> _
        Public Property AutoValidMoneyErrorMessage() As Boolean
            Set(ByVal value As Boolean)
                If value Then
                    Me.ErrorMessage = MoneyErrorMessage
                Else
                    Me.ErrorMessage = ""
                End If
            End Set
            Get
                Return Me.ErrorMessage = MoneyErrorMessage
            End Get
        End Property

        <Category("Misc")> _
        Public Property AllowNegative() As Boolean
            Set(ByVal value As Boolean)
                If value Then
                    If Me.IsValidMoney = False Then Me.IsValidMoney = True
                    Me.ValidationExpression = negativeMoneyExpression
                Else
                    Me.ValidationExpression = MoneyExpression
                End If

            End Set
            Get
                Return Me.ValidationExpression = negativeMoneyExpression
            End Get
        End Property

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.Render(writer)
        End Sub

    End Class

End Namespace

